package com.liujit.bms.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogMemberMsg;
import com.liujit.bms.app.domain.query.BlogMemberMsgQuery;
import com.liujit.bms.app.domain.result.BlogMemberMsgResult;
import com.liujit.bms.app.mapper.BlogMemberMsgMapper;
import com.liujit.bms.app.service.BlogMemberMsgService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.Pagination;
import com.liujit.web.utils.JwtUtil;
import org.springframework.stereotype.Service;

/**
 * @Description 博客会员消息Service实现类
 * @Author liujun
 * @Date 2021-07-16 15:54:46
 */
@Service
public class BlogMemberMsgServiceImpl extends ServiceImpl<BlogMemberMsgMapper, BlogMemberMsg> implements
        BlogMemberMsgService {

    @Override
    public PageBean<BlogMemberMsgResult> getPage(BlogMemberMsgQuery query) {
        query.setSortName(ColumnConstant.UPDATE_TIME);
        query.setSortName(SqlKeyword.ASC.getSqlSegment());
        Page<BlogMemberMsg> page = Pagination.page(query);
        LambdaQueryWrapper<BlogMemberMsg> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberMsg::getMemberId, JwtUtil.getUserId());
        IPage<BlogMemberMsg> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogMemberMsgResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogMemberMsg blogMemberMsg = super.getById(params.getId());
        if (blogMemberMsg == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogMemberMsg> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberMsg::getId, params.getId());
        wrapper.eq(BlogMemberMsg::getMemberId, JwtUtil.getUserId());
        return super.remove(wrapper);
    }

    @Override
    public Boolean clear() {
        LambdaQueryWrapper<BlogMemberMsg> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberMsg::getMemberId, JwtUtil.getUserId());
        return super.remove(wrapper);
    }
}
